package com.sgb.kjwl.viewmodel.self.packers;

import android.view.Gravity;
import android.view.View;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.widget.packers.SelectAreaTwoLevelPopWindow;
import com.sgb.kjwl.view.widget.packers.TwoLevelForSelectAreaPopWindow;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfServiceAreaActivity;
import com.sgb.kjwl.view.widget.TeamMultiCityPopWindow;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @author WYJ
 * @date 2020/8/27
 */
public class SelfServiceAreaModel extends BaseViewModel implements TwoLevelForSelectAreaPopWindow.SelectTwoResultClick<AddressEntity> {

    private SelfServiceAreaActivity mActivity;

    public SelectAreaTwoLevelPopWindow mPopWindow;

    private List<AddressEntity<List<AddressEntity>>> mOpt;
    /**
     * 省
     */
    private final List<AddressEntity> mOpt1 = new ArrayList<>();
    /**
     * 市
     */
    private final List<List<AddressEntity>> mOpt2 = new ArrayList<>();

    public SelfServiceAreaModel(SelfServiceAreaActivity mActivity, SelfRepertory mRepertory) {
        this.mActivity = mActivity;
    }

    public void getCityData() {
        DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getLocateList1().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if ("200".equals(response.body().getCode())) {
                    mOpt = response.body().getData();
                    if (mOpt == null || mOpt.size() == 0) {
                        return;
                    }
                    mOpt = response.body().getData();
                    mOpt1.clear();
                    mOpt1.addAll(mOpt);
                    mOpt2.clear();
                    for (AddressEntity<List<AddressEntity>> provinceDatum : mOpt) {
                        List<AddressEntity> children = provinceDatum.getChildren();
                        mOpt2.add(children);
                    }
                    updateAddress();
                } else {
                    MToast.showToast(mActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    private void updateAddress() {
        if (mPopWindow == null) {
            //初始化筛选
            mPopWindow = new SelectAreaTwoLevelPopWindow(mActivity);
            mPopWindow.setTitle("选择工作区域");
            mPopWindow.setSelectResultClick(this);
            mActivity.initCity();
        }
        mPopWindow.setTwoLevelData(mOpt1,mOpt2);
        mPopWindow.setProvinceDefaultItem(0);
    }

    public void showPop(View view) {
        if (mPopWindow == null) {
            getCityData();
        } else {
            if (!mPopWindow.isShowing()) {
                mPopWindow.showPopwindow(view, Gravity.BOTTOM,0,0);
            }
        }
    }

    @Override
    public void result(AddressEntity province, List<AddressEntity> pickSelectEntities, String name) {
        if (pickSelectEntities == null) {
            return;
        }
        int length = pickSelectEntities.size();
        if (length == 1) {
            SelfTeamIntroduceEntity.ServiceAreasBean bean = new SelfTeamIntroduceEntity.ServiceAreasBean();
            bean.setName(pickSelectEntities.get(0).getName());
            bean.setAreaNo(pickSelectEntities.get(0).getNo());
            mActivity.updateAddress(null, bean);
        } else {
            List<SelfTeamIntroduceEntity.ServiceAreasBean> beas = new ArrayList<>();
            for (AddressEntity pickSelectEntity : pickSelectEntities) {
                SelfTeamIntroduceEntity.ServiceAreasBean bean = new SelfTeamIntroduceEntity.ServiceAreasBean();
                bean.setName(pickSelectEntity.getName());
                bean.setAreaNo(pickSelectEntity.getNo());
                beas.add(bean);
            }
            mActivity.updateAddress(beas, null);
        }
    }

    @Override
    public void reset() {
        mActivity.updateAddress(null, null);
    }
}
